﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace MVVMLab
{
    public class LabContact : INotifyPropertyChanged, IDataErrorInfo
    {
        public string _firstName;
        public string FirstName
        {
            get
            {
                return _firstName;
            }
            set
            {
                if (_firstName != value)
                {
                    _firstName = value;
                    OnPropertyChanged("FirstName");
                }
            }
        }

        public string _lastName;
        public string LastName
        {
            get
            {
                return _lastName;
            }
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    OnPropertyChanged("LastName");
                }
            }
        }

        public int _numberOfComputers;
        public int NumberOfComputers
        {
            get
            {
                return _numberOfComputers;
            }
            set
            {
                if (_numberOfComputers != value)
                {
                    _numberOfComputers = value;
                    OnPropertyChanged("NumberOfComputers");
                }
            }
        }

        public DateTime? _birthday;
        public DateTime? Birthday
        {
            get
            {
                return _birthday;
            }
            set
            {
                _birthday = value;
                OnPropertyChanged("Birthday");
            }
        }


        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler ph = PropertyChanged;
            if (ph != null)
                ph(this, new PropertyChangedEventArgs(name));
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        protected Dictionary<string, string> _errors = new Dictionary<string, string>();
        public IDictionary<string, string> Errors
        {
            get { return _errors; }
        }

        public string this[string columnName]
        {
            get
            {
                string errorMessage = string.Empty;
                this.Errors.Remove(columnName);

                switch (columnName)
                {
                    case "Birthday":
                        if (this.Birthday.HasValue && this.Birthday > DateTime.Today)
                        {
                            errorMessage = "Birthday must be before today.";
                        }

                        break;
                    case "NumberOfComputers":
                        if (this.NumberOfComputers < 0)
                        {
                            errorMessage = "Number of computers must be greater than or equal to zero.";
                        }

                        break;
                    case "FirstName":
                        if (string.IsNullOrEmpty(this.FirstName) || this.FirstName.Trim().Length == 0 )
                        {
                            errorMessage = "First name is a required field.";
                        }

                        break;
                    case "LastName":
                        if (string.IsNullOrEmpty(this.LastName) || this.LastName.Trim().Length == 0)
                        {
                            errorMessage = "Last name is a required field.";
                        }

                        break;
                }

                if (!string.IsNullOrEmpty(errorMessage))
                {
                    this.Errors.Add(columnName, errorMessage);
                }

                return errorMessage;
            }
        }

        #endregion
    }
}